import { getDayNumByMonth } from '@/utils/timeUtil'
import { getRecordList, getRecordCollect } from '@/api/tabbar/detail/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    year: 0, // 选中年份
    month: 0, // 选中月份
    day: 0, // 选中天
    dayNum: 0, // 当前月份的天数
    displayMultipleItems: 7,  // 每页的swpier个数
    current: 0, // 当前选中的swiper下标
    budgetList: [], //  消费记录
    height: '0',  // 记录的盒子高度
    payInfoList: [], // 支付信息列表
    payMap: {}, // 选中月份每天收支和列表
    income: '0',  // 月收入
    expend: '0', // 月支出
  },
  // 获取当前年月
  getNowDate() {
    const currentDate = new Date()
    // 获取年份
    const year = currentDate.getFullYear()
    // 获取月份（注意月份是从0开始计数的，所以要加1）
    const month = currentDate.getMonth() + 1
    // 获取日期
    const day = currentDate.getDate()
    const dayNum = getDayNumByMonth()

    this.setData({
      year,
      month,
      day,
      current: day - 7 <= 0 ? 0 : day - 7,
      dayNum,
    })
  },
  // 获取选中日期
  getSelectDate() {
    let { year, month, day } = this.data
    if (month < 10) month = `0${month}`
    if (day < 10) day = `0${day}`
    return `${year}-${month}-${day}`
  },
  // 获取记录统计数据
  getRecordCollect() {
    getRecordCollect(this.getSelectDate()).then((res) => {
      const { income, expend, payList } = res.data
      const payMap = <Record<string, any>>{}
      for (let index = 0; index < payList.length; index++) {
        const element = payList[index];
        payMap[element.day] = element.price
      }
      this.setData({ income, expend, payMap })
    })
  },
  // 获取记录列表
  getRecordList() {
    getRecordList(this.getSelectDate()).then((res) => {
      this.setData({ payInfoList: res.data })
    })
  },
  // 选择时间
  handleSelectDayTap(e: WechatMiniprogram.CustomEvent) {
    const { day } = e.currentTarget.dataset
    this.setData({ day })
    this.getRecordList()
  },
  // 切换月份
  handleChangeMonthTap(e: WechatMiniprogram.CustomEvent) {
    const { type } = e.currentTarget.dataset
    let { month, year } = this.data
    month = type === 'next' ? month + 1 : month - 1
    const monthMap: { [key: number]: () => void } = {
      13: () => { month = 1; year += 1 },
      0: () => { month = 12; year -= 1 }
    }
    monthMap[month] && monthMap[month]()
    const dayNum = new Date(year, month, 0).getDate()
    this.setData({
      month,
      year,
      dayNum,
      day: 1,
      current: 0,

    })
    this.getRecordList()
    this.getRecordCollect()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getNowDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getHeight()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    this.getRecordList()
    this.getRecordCollect()
  },
  // 获取滚动盒子的高度
  getHeight() {
    var that = this
    const query = this.createSelectorQuery()
    query.select('#root').boundingClientRect().selectViewport()
    query.exec(function (res: any) {
      that.setData({ height: res[0].height })
    })
  }
})
